<?php 
namespace Rac\Model;
use Think\Model;
use Think\Page;

/*角色*/
class AuthGroupModel extends Model {
	
	// 返回所有的角色
	public function getGroups() {
		return $this->select();
	}
	
	
	public function setGroupRules($id, $rules) {
		$map = array('id'=>$id);
		return $this->where($map)->setField(array('rules'=>$rules));
	}
	
	public function getGroupById($id) {
		$map = array('id'=>$id);
		return $this->where($map)->find();
	}
	
	public function addAuthGroup($title, $des) {
		$data = array(
			'title'=>$title,
			'des'=>$des	
		);
		return $this->add($data);
	}
	
	public function getAuthGroupList($map) {
		$limit = get_sys_var('page_limit');
		if (empty($limit)) {
			$limit = 10;
		}
		$count      = $this->where($map)->count();
		$Page       = new Page($count, $limit);
		$show       = $Page->show();// 分页显示输出
		
		$username_field = "(
			SELECT
				GROUP_CONCAT(
					CONCAT('', admin.username, '')
				) AS a
		
			FROM
				auth_group_access
				LEFT JOIN admin 
			ON auth_group_access.uid=admin.id
			WHERE
				auth_group_access.group_id = auth_group.id
		
		) AS usernames";
		$field = "*, {$username_field}";
		$data = $this->field($field)->where($map)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		return array('list'=>$data, 'page'=>$show);
	}
	
	
	
}


?>